package com.example.photoview;

import com.jfoenix.controls.JFXTreeCell;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.control.TreeCell;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.layout.HBox;
import javafx.util.Callback;

import java.io.File;
import java.net.URL;
import java.util.ResourceBundle;
/**
 * 左侧TreeView的具体编写
 */
public class FilesTreeController implements Initializable {


    private HomeController homeControllers;

    @FXML
    public TreeView<File> FilesTree;

    public FilesTreeController(){
        ControllerUtil.controllers.put(this.getClass().getSimpleName(), this);
        homeControllers = (HomeController) ControllerUtil.controllers.get(HomeController.class.getSimpleName());

    }
    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
        initTreeView();
    }

    public void initTreeView(){
        File[] rootList = File.listRoots();
        TreeItem<File> mainTreeItem = new TreeItem<>(rootList[0]);
        for (File file:rootList){
            TreeItem<File> treeItem =new TreeItem<>(file);
            addItem(treeItem,0);
            mainTreeItem.getChildren().add(treeItem);
        }

        FilesTree.setRoot(mainTreeItem);
        FilesTree.setShowRoot(false);
        FilesTree.setCellFactory(new Callback<>() {
            @Override
            public TreeCell<File> call(TreeView<File> fileTreeView) {
                TreeCell<File> treeCell = new TreeCell<>() {
                    @Override
                    protected void updateItem(File file, boolean b) {
                        super.updateItem(file, b);
                        if (!b) {
                            this.setText(isRoot(file));
                        } else {
                            setText(null);
                        }
                        setGraphic(null);
                    }
                };
                return treeCell;
            }
        });
        FilesTree.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<TreeItem<File>>() {
            @Override
            public void changed(ObservableValue<? extends TreeItem<File>> observableValue, TreeItem<File> treeItem, TreeItem<File> t1) {
                String path = t1.getValue().getAbsolutePath();
                System.out.println(path);
                homeControllers.initEnterFolder(path);
                homeControllers.getSortComboBox().setValue(SortParam.SBNR);
                addItem(t1,0);

            }
        });
    }

    public String isRoot(File file){
        File[] rootList = File.listRoots();
        for (File isListRoots : rootList) {
            if (file.toString().equals(isListRoots.toString())) {
                return file.toString();
            }
        }
        return file.getName();
    }

    public void addItem(TreeItem<File> treeItem,int flag){
        File[] files = treeItem.getValue().listFiles();
        if (files!=null){
            if (flag==0){
                treeItem.getChildren().remove(0,treeItem.getChildren().size());
            }
            if (files.length>0){
                for (File file:files){
                    if(file.isDirectory()&!file.isHidden()){
                        TreeItem<File> newItem = new TreeItem<>(file);
                        if (flag < 1) {
                            addItem(newItem, flag + 1);
                        }
                        treeItem.getChildren().add(newItem);
                    }
                }
            }
        }}

}
